#ifndef DRMVIDEOSINK_H
#define DRMVIDEOSINK_H

#include <stdio.h>
#include <stdint.h>
#include <errno.h>
#include <drm.h>
#include <xf86drm.h>
#include <xf86drmMode.h>
#include <drm_fourcc.h>
#include "VideoSink.h"
#include "FrameQueue.h"

typedef struct Buffer_Object{
	uint32_t width;
	uint32_t height;
	uint32_t pitch;
	uint32_t handle;
	uint32_t offset;
	uint32_t size;
	uint8_t *vaddr;
	uint32_t fb_id;

	uint32_t format;
}Buffer_Object;

class DRMVideoSink : public VideoSink
{
public:
	DRMVideoSink();
	virtual ~DRMVideoSink();
	virtual int init(int width, int height, int hor_stride, int ver_stride, AVPixelFormat format);
	virtual bool isInited();
	virtual void setDisplayArea(int x, int y, int w, int h);
	virtual int showFrame(Frame *vframe);
	virtual void setProperty(const char *pro, char *val);
	virtual void dumpPicture(const char *file, int start, int count);
	virtual void keepRatio(bool keepratio);

protected:
	uint8_t *createFB();
	void freeFB();
	int findPlaneForCrtc();
	int configureModeSetting();
	uint32_t getDRMFormat(AVPixelFormat fmt);
	void caculateRatio(int &result_x, int &result_y, int &result_w, int &result_h);
	
protected:
	int m_Fd;
	drmModeConnector *m_Connector;
	drmModeRes *m_Res;
	drmModePlaneRes *m_PlaneRes;
	drmModeCrtc *m_Crtc;
	uint32_t m_ConnID;
	uint32_t m_CrtcID;
	uint32_t m_PlaneID;

	//对应AVPixFormat的drm format
	uint32_t m_DRMFormat;

	//ffmpeg硬解一般将yuv数据输出在一个buffer中，软解会由data[0] data[1] data[2]指定yuv数据
	//该值为buffer的大小(根据参数计算所得)
	uint32_t m_CopySize;

	//一个DRMbuffer对象
	Buffer_Object m_BufObj;

	//显示器分辨率
	int m_WDisplay;
	int m_HDisplay;
};

#endif